package com.pjb.springsecurity.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pjb.springsecurity.config.RedisConfig;
import com.pjb.springsecurity.domain.entity.Weather;
import com.pjb.springsecurity.mapper.WeatherMapper;
import com.pjb.springsecurity.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
public class MyController {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * redis 测试
     */
//    @RequestMapping(value = "redis")
//    public String test() throws JsonProcessingException {
//        Weather weather = new Weather();
//        weather.setCity("changning");
//        weather.setTime(new Date());
//        weather.setTemperature(String.valueOf((1 + (int) (Math.random() * 500)) / 10.0));
//        ObjectMapper mapper = new ObjectMapper();
//        String jsonData = mapper.writeValueAsString(weather);
//        System.out.println(jsonData);
//
//
//        redisTemplate.opsForValue().set("weather", jsonData);
//
//        System.out.println("redis 成功保存");
//        return "redis 保存成功！";
//    }


    @Autowired
    private WeatherMapper weatherMapper;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 接收天气信息 保存到redis
     */

    @RequestMapping(value = "/pushWeather", method = RequestMethod.POST)
    @ResponseBody
    public void getWeather(@RequestBody Weather weather) {

        try {
//            weatherMapper.save(weather);
            System.out.println(weather.toString());
            long nanoTime = System.nanoTime();
            System.out.println(nanoTime);

            redisUtil.set(weather.getCity(),weather);//按名称保存到redis，前端刷新使用

//            redisUtil.set(String.valueOf(nanoTime), weather);//保存到redis，定期取出插入到mysql

            redisUtil.set(weather.getCity() + "_" + weather.getId(), weather);

            Weather weatherRedis = (Weather) redisUtil.get(String.valueOf(nanoTime));//获取redis内的数据


            weatherMapper.save(weatherRedis);//保存到mysql

            String weatherStr = weatherRedis.toString();
            System.out.println("================================="+weatherStr);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }


    /**
     * 接收天气信息
     */
//    @RequestMapping(value = "/pushWeather", method = RequestMethod.POST)
//    @ResponseBody()
//    public void getWeather(@RequestBody Weather weather) {
//        try {
//            weatherMapper.save(weather);
//            System.out.println(weather.toString());
//        } catch (Exception e) {
//            System.out.println(e.getMessage());
//        }
//    }

    /**
     * 更新天气信息
     */
    @RequestMapping(value = "/updateWeather", method = RequestMethod.POST)
    @ResponseBody()
    public void updateWeather(@RequestBody Weather weather) {
        try {

            List<Weather>  weatherList= weatherMapper.getWeather(weather.getCity());
            if (weatherList!=null&&weatherList.size()>0) {
                Weather lastWeather=weatherList.get(weatherList.size()-1);
                weather.setId(lastWeather.getId());
                weatherMapper.updateWeather(weather);
                System.out.println("更新数据："+lastWeather.toString()+" ----->    "+weather.toString());
            }else {
                weatherMapper.save(weather);
                System.out.println("保存数据："+weather.toString());
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * 根据城市名查询天气
     */
    @RequestMapping(value = "/getWeather", method = RequestMethod.POST)
    @ResponseBody()
    public Object getWeather(@RequestParam String cityname, Model model, HttpServletRequest request) {
        System.out.println(cityname);
        List<Weather> weathers = weatherMapper.getWeather(cityname);
        if (weathers != null & weathers.size() > 0) {
            for (Weather weather : weathers) {
                System.out.println(weather.toString());
            }
            return weathers;
        } else {
            return "未查询到数据";
        }
    }

    /**
     * 根据城市名查询天气
     */
    @RequestMapping(value = "/weather", method = RequestMethod.GET)
    @ResponseBody()
    public Object updateWeather() {
        List<Weather> weathers = weatherMapper.getWeather("changning");
        if (weathers != null & weathers.size() > 0) {
            for (Weather weather : weathers) {
                System.out.println(weather.toString());
            }
            return weathers;
        } else {
            return "未查询到数据";
        }
    }

}
